﻿using Domain;
using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
	public class MCRepository : IMCRepository
	{
		private readonly MyDbContext myDbContext;

		public MCRepository(MyDbContext myDbContext)
        {
			this.myDbContext = myDbContext;
		}

		public async Task<bool> AddMc(MC mC)
		{
			myDbContext.MC.Add(mC);
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public MCFenYe DataMC(string firstKindName, string secondKindName, string thirdKindName, DateTime startDate, DateTime endDate, int page, int pageSize)
		{
			var query = myDbContext.MC.AsQueryable();

			if (!string.IsNullOrEmpty(firstKindName))
			{
				query = query.Where(s => s.FirstKindName == firstKindName);
			}
			if (!string.IsNullOrEmpty(secondKindName))
			{
				query = query.Where(s => s.SecondKindName == secondKindName);
			}
			if (!string.IsNullOrEmpty(thirdKindName))
			{
				query = query.Where(s => s.ThirdKindName == thirdKindName);
			}
			if (startDate != default && endDate != default)
			{
				query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
			}

			int totalRecords = query.Count();
			var MC = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new MCFenYe
			{
				totalRecords = totalRecords,
				mc = MC
			};
		}

		public MCFenYe GetMcByCheckReason(string checkReason, int page, int pageSize)
		{
			var query = myDbContext.MC.Where(e=>e.CheckReason==checkReason).AsQueryable();
			int totalRecords = query.Count();
			var MC = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new MCFenYe
			{
				totalRecords = totalRecords,
				mc = MC
			};
		}

		public MCFenYe SelectMC(string checkReason, string newfirstKindName, string newsecondKindName, string newthirdKindName, string newmajorKindName, string newmajorName, DateTime startDate, DateTime endDate, int page, int pageSize)
		{
			var query = myDbContext.MC.Where(e => e.CheckReason == checkReason).AsQueryable();

			if (!string.IsNullOrEmpty(newfirstKindName))
			{
				query = query.Where(s => s.NewFirstKindName == newfirstKindName);
			}
			if (!string.IsNullOrEmpty(newsecondKindName))
			{
				query = query.Where(s => s.NewSecondKindName == newsecondKindName);
			}
			if (!string.IsNullOrEmpty(newthirdKindName))
			{
				query = query.Where(s => s.NewThirdkindName == newthirdKindName);
			}
			if (!string.IsNullOrEmpty(newmajorKindName))
			{
				query = query.Where(s => s.NewMajorKindName == newmajorKindName);
			}
			if (!string.IsNullOrEmpty(newmajorName))
			{
				query = query.Where(s => s.NewMajorName == newmajorName);
			}
			if (startDate != default && endDate != default)
			{
				query = query.Where(s => s.RegistTime >= startDate && s.RegistTime <= endDate);
			}

			int totalRecords = query.Count();
			var MC = query.Skip((page - 1) * pageSize).Take(pageSize).ToList();
			return new MCFenYe
			{
				totalRecords = totalRecords,
				mc = MC
			};
		}

		public async Task<bool> UpdateMC(MC mc)
		{
			MC mcs = myDbContext.MC.Where(e => e.MchId == mc.MchId).FirstOrDefault();
			if (mcs!=null)
			{
				mcs.NewFirstKindId = mc.NewFirstKindId;
				mcs.NewFirstKindName = mc.NewFirstKindName;
				mcs.NewSecondKindId = mc.NewSecondKindId;
				mcs.NewSecondKindName = mc.NewSecondKindName;
				mcs.NewThirdKindId = mc.NewThirdKindId;
				mcs.NewThirdkindName = mc.NewThirdkindName;
				mcs.NewMajorKindId = mc.NewMajorKindId;
				mcs.NewMajorKindName = mc.NewMajorKindName;
				mcs.NewMajorId = mc.NewMajorId;
				mcs.NewMajorName = mc.NewMajorName;
				mcs.NewSalaryStandardId = mc.NewSalaryStandardId;
				mcs.NewSalaryStandardName = mc.NewSalaryStandardName;
				mcs.NewSalarySum = mc.NewSalarySum;
				mcs.Register = mc.Register;
				mcs.RegistTime = mc.RegistTime;
				mcs.ChangeReason = mc.ChangeReason;
				mcs.CheckReason = mc.CheckReason;
				myDbContext.MC.Update(mcs);
			}
			
			return await myDbContext.SaveChangesAsync() > 0;
		}

		public async Task<bool> UpdateReviewMC(MC mc)
		{
			MC mcs = myDbContext.MC.Where(e => e.MchId == mc.MchId).FirstOrDefault();
			if (mcs != null)
			{
				mcs.CheckStatus = mc.CheckStatus;
				mcs.Checker = mc.Checker;
				mcs.CheckTime = mc.CheckTime;
				mcs.CheckReason = "2";
				myDbContext.MC.Update(mcs);
			}
			return await myDbContext.SaveChangesAsync() > 0;
		}
	}

}
